//
//  BaseSourceChildViewController.m
//  AnXin100
//
//  Created by Sakura on 2018/11/13.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "BaseSourceChildViewController.h"
#import "BaseSourceChildCollectionViewCell.h"
#import "ReleaseViewController.h"
@interface BaseSourceChildViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *moveRedPacket;
@end

@implementation BaseSourceChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self CreatMoveRedPacketUI];
    self.view.backgroundColor = [UIColor whiteColor];

}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)search:(UIButton *)btn {
    
}


- (void)creatView {
    
    
    /** collectionView */
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-30)/2.0, (SCREEN_WIDTH-30)/2.0);
    /** 最小行间距 */
    flowLayout.minimumLineSpacing = 10;
    /** 最小列间距 */
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - Hn - 44) collectionViewLayout:flowLayout];

    self.collectionView.contentInset = UIEdgeInsetsMake(8, 8, 8, 8);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[BaseSourceChildCollectionViewCell class] forCellWithReuseIdentifier:@"BaseSourceChildCollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
}



#pragma mark - Collection View Data Source
/** 定义展示的section的个数 */

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
/** 定义展示的collectionCell的个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return 8;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseSourceChildCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BaseSourceChildCollectionViewCell" forIndexPath:indexPath];
    return cell;
}

-(void)CreatMoveRedPacketUI{
    
    UIPanGestureRecognizer *panTouch = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    if (_moveRedPacket==nil) {
        _moveRedPacket = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 50, SCREEN_HEIGHT - 200, 40, 40)];
        [_moveRedPacket setTitle:@"发布" forState:UIControlStateNormal];
        _moveRedPacket.titleLabel.font = FONT_FOURTEEN;
        _moveRedPacket.layer.cornerRadius = 20;
    }
//    [_moveRedPacket setBackgroundImage:[UIImage imageNamed:@"red"] forState:UIControlStateNormal];
    _moveRedPacket.backgroundColor = COLOR_GREEN;
    [_moveRedPacket addTarget:self action:@selector(ClickRedPacket:) forControlEvents:UIControlEventTouchUpInside];
    [_moveRedPacket addGestureRecognizer:panTouch];
    [self.view addSubview:_moveRedPacket];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    
        //视图前置操作
    
        [recognizer.view.superview bringSubviewToFront:recognizer.view];
    
        
    
        CGPoint center = recognizer.view.center;
    
        CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
    
        CGPoint translation = [recognizer translationInView:self.view];
    
        //NSLog(@"%@", NSStringFromCGPoint(translation));
    
        recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
    
        [recognizer setTranslation:CGPointZero inView:self.view];
    
        
    
        if (recognizer.state == UIGestureRecognizerStateEnded) {
        
                //计算速度向量的长度，当他小于200时，滑行会很短
        
                CGPoint velocity = [recognizer velocityInView:self.view];
        
                CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        
                CGFloat slideMult = magnitude / 200;
        
                //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
        
                
        
                //基于速度和速度因素计算一个终点
        
                float slideFactor = 0.1 * slideMult;
        
                CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
                                                 
                                                                                          center.y + (velocity.y * slideFactor));
        
                //限制最小［cornerRadius］和最大边界值［self.view.bounds.size.width - cornerRadius］，以免拖动出屏幕界限
        
                finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
                                   
                                                              self.view.bounds.size.width - cornerRadius);
        
                finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
                                   
                                                              self.view.bounds.size.height - cornerRadius);
        
                
        
                //使用 UIView 动画使 view 滑行到终点
        
                [UIView animateWithDuration:slideFactor*2
                 
                                               delay:0
                 
                                             options:UIViewAnimationOptionCurveEaseOut
                 
                                          animations:^{
                     
                                                  recognizer.view.center = finalPoint;
                     
                                              }
                 
                                          completion:nil];
        
            }
    
}

- (void)ClickRedPacket:(UIButton *)sender {
    ReleaseViewController *vc = [ReleaseViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
